//
//  SWEffectCollectionCell.swift
//  ChildStory
//
//  Created by linhan on 16/8/29.
//  Copyright © 2016年 Aiya. All rights reserved.
//

import Foundation
import UIKit

class SWCameraCollectionCell: UICollectionViewCell
{
    weak var delegate:CollectionViewCellDelegate?
    
    private var _change:Bool = true
    private var _imageView:UIImageView = UIImageView()
    
    var photo:SWCameraPhoto?{
        didSet {
            layoutSubviews()
        }
    }
    
    override init(frame: CGRect)
    {
        super.init(frame: frame)
        setup()
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setup()
    {
        contentView.clipsToBounds = true
        contentView.backgroundColor = UIColor(white: 0.97, alpha: 1)
        
        _imageView.frame = bounds
        _imageView.contentMode = .scaleAspectFill
        contentView.addSubview(_imageView)
        
        let deleteBtn:UIButton = UIButton(type:.system)
        deleteBtn.setTitle("X", for: .normal)
        deleteBtn.sizeToFit()
        deleteBtn.frame = CGRectMake(width - deleteBtn.width, 0, deleteBtn.width, deleteBtn.height)
        deleteBtn.addTarget(self, action: #selector(deleteBtnTapped), for: .touchUpInside)
        contentView.addSubview(deleteBtn)
    }
    
    override func prepareForReuse()
    {
        super.prepareForReuse()
        _change = true
    }
    
    override func layoutSubviews()
    {
        super.layoutSubviews()
        if let photo = photo
        {
            _imageView.image = photo.thumbImage ?? photo.largeImage
        }
    }
    
    @objc private func deleteBtnTapped()
    {
        delegate?.collectionViewCell(self, click: CallbackAction.Delete, argument: nil)
    }
    
    
    
}
